/********************************************************************
* @Copyright: Metanergy Technology R&D Co., Ltd
* @Filename : myg0025_comp.c
* @brief    : API file of comp module
* @Author   : AE Team
* @Version  : V1.0.0/2023-03-30
* @log  V1.0.0/2023-03-30 Initial release
 ********************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "myg0025_comp.h"
#include "myg0025_rcc.h"

/** @addtogroup myg0025_StdPeriph_Driver
  * @{
  */


/** @defgroup COMP COMP
  * @brief COMP modules
  * @{
  */

/** @defgroup COMP_Private_Functions COMP_Private_Functions
  * @{
  */


/**
  * @brief  Deinitializes the COMP peripheral registers to their default reset values.
  * @retval None.
  */
void COMP_DeInit(void)
{
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_COMP, ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_COMP, DISABLE);
}

/**
  * @brief  Fills each COMP_InitStruct member with its default value.
  * @param  COMP_InitStruct: pointer to a COMP_InitStruct structure which will be initialized.
  * @retval None
  */
void COMP_StructInit(COMP_InitTypeDef *COMP_InitStruct)
{
    /*--------------- Reset COMP init structure parameters values -----------------*/
    COMP_InitStruct->PowerMode = 0;
    COMP_InitStruct->COMPNInSel = 0;
    COMP_InitStruct->COMPPInSel = 0;
    COMP_InitStruct->WndwEn = 0;
    COMP_InitStruct->OutputPolarity = 0;
    COMP_InitStruct->Hysteresis = 0;
}

/**
  * @brief  Initialize some features of COMP instance.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  COMP_InitStruct: pointer to a COMP_InitTypeDef structure.
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: COMP registers are initialized.
  *          - ERROR: COMP registers are not initialized.
  */
ErrorStatus COMP_Init(uint8_t COMPx, COMP_InitTypeDef *COMP_InitStruct)
{
    ErrorStatus status = SUCCESS;

    if (COMP_IsLocked(COMPx) == 0U)
    {
        if (COMPx == COMP1)
        {
            COMP->CSR1 &= ~(COMP_CSR1_COMP1MODE
                            | COMP_CSR1_COMP1NINSEL
                            | COMP_CSR1_WNDWEN1
                            | COMP_CSR1_COMP1PINSEL
                            | COMP_CSR1_COMP1POL
                            | COMP_CSR1_COMP1HYST
                           );
            COMP->CSR1 |= (COMP_InitStruct->PowerMode
                           | COMP_InitStruct->COMPNInSel
                           | COMP_InitStruct->WndwEn
                           | COMP_InitStruct->COMPPInSel
                           | COMP_InitStruct->OutputPolarity
                           | COMP_InitStruct->Hysteresis
                          );
        }
        else if (COMPx == COMP2)
        {
            COMP->CSR1 &= ~(COMP_CSR1_COMP2MODE
                            | COMP_CSR1_COMP2NINSEL
                            | COMP_CSR1_WNDWEN2
                            | COMP_CSR1_COMP2PINSEL
                            | COMP_CSR1_COMP2POL
                            | COMP_CSR1_COMP2HYST
                           );
            COMP->CSR1 |= (COMP_InitStruct->PowerMode
                           | COMP_InitStruct->COMPNInSel
                           | COMP_InitStruct->WndwEn
                           | COMP_InitStruct->COMPPInSel
                           | COMP_InitStruct->OutputPolarity
                           | COMP_InitStruct->Hysteresis
                          );
        }
        else if (COMPx == COMP3)
        {
            COMP->CSR2 &= ~(COMP_CSR2_COMP3MODE
                            | COMP_CSR2_COMP3NINSEL
                            | COMP_CSR2_COMP3PINSEL
                            | COMP_CSR2_COMP3POL
                            | COMP_CSR2_COMP3HYST
                           );
            COMP->CSR2 |= (COMP_InitStruct->PowerMode
                           | COMP_InitStruct->COMPNInSel
                           | COMP_InitStruct->COMPPInSel
                           | COMP_InitStruct->OutputPolarity
                           | COMP_InitStruct->Hysteresis
                          );
        }
        else if (COMPx == COMP4)
        {
            COMP->CSR2 &= ~(COMP_CSR2_COMP4MODE
                            | COMP_CSR2_COMP4NINSEL
                            | COMP_CSR2_COMP4PINSEL
                            | COMP_CSR2_COMP4POL
                            | COMP_CSR2_COMP4HYST
                           );
            COMP->CSR2 |= (COMP_InitStruct->PowerMode
                           | COMP_InitStruct->COMPNInSel
                           | COMP_InitStruct->COMPPInSel
                           | COMP_InitStruct->OutputPolarity
                           | COMP_InitStruct->Hysteresis
                          );
        }
    }
    else
    {
        /* Initialization error: COMP instance is locked                         */
        status = ERROR;
    }

    return status;
}

/**
  * @brief  Set comparators Positive input connection.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  InputNonInverting:
  *         COMP1_INPUT_POSITIVE_PA14.(OAMPx_PGA_N)
  *         COMP1_INPUT_POSITIVE_PA6.
  *         COMP2_INPUT_POSITIVE_PA14.(OAMPx_PGA_N)
  *         COMP2_INPUT_POSITIVE_PA7.
  *         COMP3_INPUT_POSITIVE_PA14.(OAMPx_PGA_N)
  *         COMP3_INPUT_POSITIVE_PA8.
  *         COMP4_INPUT_POSITIVE_PA14.(OAMPx_PGA_N)
  *         COMP4_INPUT_POSITIVE_PA9.
  * @retval None.
  */
void COMP_SetInputNonInverting(uint8_t COMPx, uint32_t InputNonInverting)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1PINSEL;
        COMP->CSR1 |= InputNonInverting;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2PINSEL;
        COMP->CSR1 |= InputNonInverting;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3PINSEL;
        COMP->CSR2 |= InputNonInverting;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4PINSEL;
        COMP->CSR2 |= InputNonInverting;
    }
}

/**
  * @brief  Get comparators non-inverting input connection.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparators non-inverting input connection.
  */
uint32_t COMP_GetInputNonInverting(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1PINSEL)) >> COMP_CSR1_COMP1PINSEL_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2PINSEL)) >> COMP_CSR1_COMP2PINSEL_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3PINSEL)) >> COMP_CSR2_COMP3PINSEL_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4PINSEL)) >> COMP_CSR2_COMP4PINSEL_Pos;
    }
    return Temp;
}

/**
  * @brief  Set comparators negative input connection.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  InputInverting:
  *         COMP1_INPUT_NEGATIVE_PA10.
  *         COMP1_INPUT_NEGATIVE_PC8.
  *         COMP1_INPUT_NEGATIVE_DAC1_VOUT.
  *         COMP2_INPUT_NEGATIVE_PA10.
  *         COMP2_INPUT_NEGATIVE_PC9.
  *         COMP2_INPUT_NEGATIVE_DAC1_VOUT.
  *         COMP3_INPUT_NEGATIVE_PA10
  *         COMP3_INPUT_NEGATIVE_PC10
  *         COMP3_INPUT_NEGATIVE_DAC1_VOUT.
  *         COMP4_INPUT_NEGATIVE_PA10
  *         COMP4_INPUT_NEGATIVE_PC11
  *         COMP4_INPUT_NEGATIVE_DAC1_VOUT.
  * @retval None.
  */
void COMP_SetInputInverting(uint8_t COMPx, uint32_t InputInverting)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1NINSEL;
        COMP->CSR1 |= InputInverting;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2NINSEL;
        COMP->CSR1 |= InputInverting;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3NINSEL;
        COMP->CSR2 |= InputInverting;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4NINSEL;
        COMP->CSR2 |= InputInverting;
    }
}

/**
  * @brief  Get comparators inverting input connection.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparators inverting input connection.
  */
uint32_t COMP_GetInputInverting(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1NINSEL)) >> COMP_CSR1_COMP1NINSEL_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2NINSEL)) >> COMP_CSR1_COMP2NINSEL_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3NINSEL)) >> COMP_CSR2_COMP3NINSEL_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4NINSEL)) >> COMP_CSR2_COMP4NINSEL_Pos;
    }
    return Temp;
}

/**
  * @brief  Set window mode of a pair of comparators instances.
  * @param  WindowMode:
  *         COMP1_WINDOWOUT_DISABLE.
  *         COMP1_WINDOWOUT_ENABLE.//use COMP2 INPUT plus as window mode input
  *         COMP2_WINDOWOUT_DISABLE.
  *         COMP2_WINDOWOUT_ENABLE. //use COMP1 INPUT plus as window mode input
  * @retval None.
  */
void COMP_SetWndwEn(uint32_t WindowMode)
{
    COMP->CSR1 &= ~(COMP_CSR1_WNDWEN1 | COMP_CSR1_WNDWEN2);
    COMP->CSR1 |= WindowMode;
}

/**
  * @brief  Get window mode of a pair of comparators instances.
  * @param  COMPx: COMP1,COMP2.
  * @retval Window mode of a pair of comparators instances.
  */

uint32_t COMP_GetWndwEn(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_WNDWEN1)) >> COMP_CSR1_WNDWEN1_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_WNDWEN2)) >> COMP_CSR1_WNDWEN2_Pos;
    }

    return Temp;
}

/**
  * @brief  Set comparator instance operating mode to adjust power and speed.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  PowerMode:
  *         COMP1_FULL_POWER.
  *         COMP1_LOW_POWER.
  *         COMP2_FULL_POWER.
  *         COMP2_LOW_POWER.
  *         COMP3_FULL_POWER.
  *         COMP3_LOW_POWER.
  *         COMP4_FULL_POWER.
  *         COMP4_LOW_POWER.
  * @retval None.
  */
void COMP_SetPowerMode(uint8_t COMPx, uint32_t PowerMode)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1MODE;
        COMP->CSR1 |= PowerMode;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2MODE;
        COMP->CSR1 |= PowerMode;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3MODE;
        COMP->CSR2 |= PowerMode;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4MODE;
        COMP->CSR2 |= PowerMode;
    }
}

/**
  * @brief  Get comparator instance operating mode to adjust power and speed.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator instance operating mode to adjust power and speed.
  */
uint32_t COMP_GetPowerMode(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1MODE)) >> COMP_CSR1_COMP1MODE_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2MODE)) >> COMP_CSR1_COMP2MODE_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3MODE)) >> COMP_CSR2_COMP3MODE_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4MODE)) >> COMP_CSR2_COMP4MODE_Pos;
    }
    return Temp;
}

/**
  * @brief  Set comparator instance output polarity.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  OutputPolarity:
  *         COMP1_OUTPUTPOL_NONINVERTED.
  *         COMP1_OUTPUTPOL_INVERTED.
  *         COMP2_OUTPUTPOL_NONINVERTED.
  *         COMP2_OUTPUTPOL_INVERTED.
  *         COMP3_OUTPUTPOL_NONINVERTED.
  *         COMP3_OUTPUTPOL_INVERTED.
  *         COMP4_OUTPUTPOL_NONINVERTED.
  *         COMP4_OUTPUTPOL_INVERTED.
  * @retval None.
  */
void COMP_SetOutputPolarity(uint8_t COMPx, uint32_t OutputPolarity)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1POL;
        COMP->CSR1 |= OutputPolarity;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2POL;
        COMP->CSR1 |= OutputPolarity;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3POL;
        COMP->CSR2 |= OutputPolarity;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4POL;
        COMP->CSR2 |= OutputPolarity;
    }
}

/**
  * @brief  Get comparator instance output polarity.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator instance output polarity.
  */
uint32_t COMP_GetOutputPolarity(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1POL)) >> COMP_CSR1_COMP1POL_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2POL)) >> COMP_CSR1_COMP2POL_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3POL)) >> COMP_CSR2_COMP3POL_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4POL)) >> COMP_CSR2_COMP4POL_Pos;
    }
    return Temp;
}

/**
  * @brief  Set comparator hysteresis.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @param  Hysteresis:
  *         COMP1_HYSTERESIS_DISABLE.
  *         COMP1_HYSTERESIS_LEVEL1.
  *         COMP1_HYSTERESIS_LEVEL2.
  *         COMP1_HYSTERESIS_LEVEL3.
  *         COMP2_HYSTERESIS_DISABLE.
  *         COMP2_HYSTERESIS_LEVEL1.
  *         COMP2_HYSTERESIS_LEVEL2.
  *         COMP2_HYSTERESIS_LEVEL3.
  *         COMP3_HYSTERESIS_DISABLE.
  *         COMP3_HYSTERESIS_LEVEL1.
  *         COMP3_HYSTERESIS_LEVEL2.
  *         COMP3_HYSTERESIS_LEVEL3.
  *         COMP4_HYSTERESIS_DISABLE.
  *         COMP4_HYSTERESIS_LEVEL1.
  *         COMP4_HYSTERESIS_LEVEL2.
  *         COMP4_HYSTERESIS_LEVEL3.
  * @retval None.
  */
void COMP_SetHysteresis(uint8_t COMPx, uint32_t Hysteresis)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1HYST;
        COMP->CSR1 |= Hysteresis;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2HYST;
        COMP->CSR1 |= Hysteresis;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3HYST;
        COMP->CSR2 |= Hysteresis;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4HYST;
        COMP->CSR2 |= Hysteresis;
    }
}

/**
  * @brief  Get comparator hysteresis.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator hysteresis.
  */
uint32_t COMP_GetHysteresis(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1HYST)) >> COMP_CSR1_COMP1HYST_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2HYST)) >> COMP_CSR1_COMP2HYST_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3HYST)) >> COMP_CSR2_COMP3HYST_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4HYST)) >> COMP_CSR2_COMP4HYST_Pos;
    }
    return Temp;
}

/**
  * @brief  Enable comparator.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval None.
  */
void COMP_Enable(uint8_t COMPx)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1EN;
        COMP->CSR1 |= COMP_CSR1_COMP1EN;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2EN;
        COMP->CSR1 |= COMP_CSR1_COMP2EN;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3EN;
        COMP->CSR2 |= COMP_CSR2_COMP3EN;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4EN;
        COMP->CSR2 |= COMP_CSR2_COMP4EN;
    }
}

/**
  * @brief  Disable comparator.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval None.
  */
void COMP_Disable(uint8_t COMPx)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1EN;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2EN;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3EN;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4EN;
    }

}


/**
  * @brief  Get comparator enable state.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator enable state.
  */
uint32_t COMP_IsEnabled(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1EN)) >> COMP_CSR1_COMP1EN_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2EN)) >> COMP_CSR1_COMP2EN_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3EN)) >> COMP_CSR2_COMP3EN_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4EN)) >> COMP_CSR2_COMP4EN_Pos;
    }
    return Temp;
}

/**
  * @brief  Lock comparator.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval None.
  */
void COMP_Lock(uint8_t COMPx)
{
    if (COMPx == COMP1)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP1LOCK;
        COMP->CSR1 |= COMP_CSR1_COMP1LOCK;
    }
    else if (COMPx == COMP2)
    {
        COMP->CSR1 &= ~COMP_CSR1_COMP2LOCK;
        COMP->CSR1 |= COMP_CSR1_COMP2LOCK;
    }
    else if (COMPx == COMP3)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP3LOCK;
        COMP->CSR2 |= COMP_CSR2_COMP3LOCK;
    }
    else if (COMPx == COMP4)
    {
        COMP->CSR2 &= ~COMP_CSR2_COMP4LOCK;
        COMP->CSR2 |= COMP_CSR2_COMP4LOCK;
    }
}

/**
  * @brief  Get comparator lock state.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator lock state.
  */
uint32_t COMP_IsLocked(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1LOCK)) >> COMP_CSR1_COMP1LOCK_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2LOCK)) >> COMP_CSR1_COMP2LOCK_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3LOCK)) >> COMP_CSR2_COMP3LOCK_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4LOCK)) >> COMP_CSR2_COMP4LOCK_Pos;
    }
    return Temp;
}

/**
  * @brief  Read comparator instance output level.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  * @retval Comparator instance output level.
  */
uint32_t COMP_ReadOutputLevel(uint8_t COMPx)
{
    uint32_t Temp;
    if (COMPx == COMP1)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP1OUT)) >> COMP_CSR1_COMP1OUT_Pos;
    }
    else if (COMPx == COMP2)
    {
        Temp = (uint32_t)((COMP->CSR1 & COMP_CSR1_COMP2OUT)) >> COMP_CSR1_COMP2OUT_Pos;
    }
    else if (COMPx == COMP3)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP3OUT)) >> COMP_CSR2_COMP3OUT_Pos;
    }
    else if (COMPx == COMP4)
    {
        Temp = (uint32_t)((COMP->CSR2 & COMP_CSR2_COMP4OUT)) >> COMP_CSR2_COMP4OUT_Pos;
    }
    return Temp;
}

/**
  * @brief  Set COMP filer Clock Count.
  * @param  FilerCLKCnt:
  *         1~65535: FilerCLKCnt
  */
void COMP_SetFilerCnt(uint16_t FilerCLKCnt)
{
    uint32_t temp_flt_ctrl = COMP->FLT_CTRL & (COMP_FLT_CTRL_COMP1FEN_Mask | COMP_FLT_CTRL_COMP2FEN_Mask | COMP_FLT_CTRL_COMP3FEN_Mask | COMP_FLT_CTRL_COMP4FEN_Mask);
    COMP->FLT_CTRL = 0;
    COMP->FLT_CTRL = FilerCLKCnt | temp_flt_ctrl;

    return;
}


/**
  * @brief  Set COMP filer Enable.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  */
void COMP_SetFilerEnable(uint8_t COMPx)
{
    if (COMPx == COMP1)
    {
        COMP->FLT_CTRL |= COMP_FLT_CTRL_COMP1FEN;
    }
    else if (COMPx == COMP2)
    {
        COMP->FLT_CTRL |= COMP_FLT_CTRL_COMP2FEN;
    }
    else if (COMPx == COMP3)
    {
        COMP->FLT_CTRL |= COMP_FLT_CTRL_COMP3FEN;
    }
    else if (COMPx == COMP4)
    {
        COMP->FLT_CTRL |= COMP_FLT_CTRL_COMP4FEN;
    }
    return;
}
/**
  * @brief  Set COMP filer disable.
  * @param  COMPx: COMP1,COMP2,COMP3,COMP4.
  */
void COMP_SetFilerDisable(uint8_t COMPx)
{
    if (COMPx == COMP1)
    {
        COMP->FLT_CTRL &= ~COMP_FLT_CTRL_COMP1FEN;
    }
    else if (COMPx == COMP2)
    {
        COMP->FLT_CTRL &= ~COMP_FLT_CTRL_COMP2FEN;
    }
    else if (COMPx == COMP3)
    {
        COMP->FLT_CTRL &= ~COMP_FLT_CTRL_COMP3FEN;
    }
    else if (COMPx == COMP4)
    {
        COMP->FLT_CTRL &= ~COMP_FLT_CTRL_COMP4FEN;
    }
    return;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
